home *** CD-ROM | disk | FTP | other *** search
- # include "nrs.h"
-
- # ifndef lint
- static char RCSid[]="@(#)$Header: misc.c,v 3.6 88/09/08 17:39:18 pb Exp $";
- # endif
-
- /***********************************************************************\
- * *
- * The various miscellaneous routines that one finds in almost *
- * any program, but there are probably too many! *
- * *
- * Copyright 1986, Piete Brooks, Julian Onions & Adrian Pell *
- * pb@cl.cam.ac.uk, jpo@hcig.nott.ac.uk, Adrian.R.Pell@reading.ac.uk *
- * *
- * This program may be copied as long as you don't remove this notice, *
- * try to make any money off of it, or pretend that you wrote it. *
- * *
- \***********************************************************************/
-
- extern errno;
-
- static_f void pmesg_3();
- static_f void getversion();
- static_f void verprint();
-
- /* VARARGS 1 */
- void fatal_2(fmt, a1,a2) /* a fatal error */
- char *fmt;
- universaldec(a1)
- universaldec(a2)
- {
- pmesg_3(fmt,a1,a2,universaluse(0));
- exit(exit_rc(1));
- }
-
- /* VARARGS 1 */
- void warn_3(level, fmt, a1, a2, a3) /* a warning message */
- char *fmt;
- universaldec(a1)
- universaldec(a2)
- universaldec(a3)
- {
- char buffer[128];
-
- warns += level;
-
- if (warns <= level*5 || warns > max_warns)
- { VOID sprintf( buffer, "warning - %s", fmt);
- pmesg_3(buffer, a1, a2, a3);
- }
-
- if (warns > max_warns)
- {
- pmesg_3("Too many warnings (%d > %d), aborting",
- warns, max_warns, 0);
- pmesg_3("Fix the problem, or try running with \"level %d\"",
- warns*2, 0, 0);
- exit(exit_rc(1));
- }
- }
-
- /* VARARGS 1 */
- static_f void pmesg_3(fmt, a1,a2,a3) /* print a message */
- char *fmt;
- universaldec(a1)
- universaldec(a2)
- universaldec(a3)
- {
- extern noshare int errno;
- #ifndef PRIME
- extern noshare int sys_nerr;
- extern noshare char *sys_errlist[];
- #endif PRIME
-
- fprintf( stderr, "%s : ", invo_name);
- fprintf( stderr, fmt, a1, a2, a3);
- #ifdef PRIME
- if (errno > 0)
- { putc(' ', stderr);
- perror("");
- }
- else putc('\n', stderr);
- #else PRIME
- if( errno > 0 && errno < sys_nerr)
- fprintf(stderr, " [%s]", sys_errlist[errno]);
- putc('\n', stderr);
- #endif
- errno = 0;
- }
-
- /* VARARGS 1 */
- void vprintf_3(level, fmt, a1,a2,a3) /* a warning message */
- int level;
- char *fmt;
- universaldec(a1)
- universaldec(a2)
- universaldec(a3)
- {
- if( level > verbose )
- return;
- fprintf( stderr, fmt, a1, a2, a3);
- }
-
- int getint(fp, n) /* get an int from a field on n */
- register FILE *fp;
- register int n;
- {
- register int value = 0, c;
-
- while(( c = getc(fp)) == '\n' || c == '\r')
- ;
- while(n -- > 0)
- {
- if( isdigit(c) )
- value = value * 10 + (c - '0');
- else if (c == '\n')
- { VOID ungetc(c, fp);
- break;
- }
- if( n > 0)
- c = getc(fp);
- }
- return value;
- }
-
- /* get a string of len chars, skipping skip chars first */
- void getstr(fp, cp, skip, len)
- register FILE *fp;
- register char *cp;
- int skip;
- int len;
- {
- char *base=cp;
- int c;
-
- while((c = getc(fp)) == '\n' || c == '\r')
- ;
- VOID ungetc(c, fp);
- while( skip -- > 0)
- c = getc(fp);
- for(; len -- > 0; cp++)
- { *cp = getc(fp);
- if (*cp == '\n')
- { ungetc(*cp, stdin);
- *cp = '\0';
- errno = 0; /* no perror() ... */
- warn2(1,
- "Inconsistant database -- string '%s' too short by %d characters",
- base, len+1);
- break;
- }
- }
- *cp = '\0';
- }
-
- void freeoff( ap ) /* throw away an atomic record */
- register Atomic *ap;
- {
- int i;
- char **p;
-
- if( ap->dte )
- {
- if (ap->dte != nullstring) free(ap->dte);
- ap->dte = (char *) 0;
- }
- if( ap->ybts)
- {
- if (ap->ybts != nullstring) free(ap->ybts);
- ap->ybts = (char *) 0;
- }
- for( i = 0, p = ap->ar; i < ap->n_relays; i++, p++)
- {
- if (*p != nullstring) free(*p);
- *p = (char *) 0;
- }
- ap->n_relays = 0;
- for( i = 0, p = ap->desc; i < ap->n_descs; i++, p++)
- {
- if (*p != nullstring) free(*p);
- *p = (char *) 0;
- }
- ap->n_descs = 0;
- }
-
- #ifndef gettab
- Table *gettab(n) /* find a table given a file number */
- register int n;
- {
- register Table *tp;
-
- if (n > fnumb)
- { vprintf2(4, "gettab passed %d (%d)\n", n, fnumb);
- n += 2 - fnumb;
- }
- if (n < MAX_FNUMB) return fnumb2trip[n].f_tp;
-
- for( tp = tablist; tp->T_name != NULL; tp++)
- {
- if( tp->hdr.fnumber == n)
- return tp;
- }
- return (Table *)0;
- }
- #endif gettab
-
- char *strdup(s) /* malloc a string */
- char *s;
- {
- int len = strlen(s);
- char *p;
-
- if( (p = malloc((unsigned) (len + 1))) == NULL)
- fatal0("Out of core/moss/memory");
- return strcpy(p,s);
- }
-
- static char chartab[] = { /* table for case independant comparisons */
- '\0', '\1', '\2', '\3', '\4', '\5', '\6', '\7',
- '\10', '\t', '\n', '\13', '\14', '\r', '\16', '\17',
- '\20', '\21', '\22', '\23', '\24', '\25', '\26', '\27',
- '\30', '\31', '\32', '\33', '\34', '\35', '\36', '\37',
- ' ', '!', '"', '#', '$', '%', '&', '\47',
- '(', ')', '*', '+', ',', '-', '.', '/',
- '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', ':', ';', '<', '=', '>', '?',
- '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
- 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
- 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
- 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
- '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
- 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
- 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
- 'X', 'Y', 'Z', '{', '|', '}', '~', '\177',
- };
-
- int lexequ(s1,s2,n) /* = strncmp + case insensitive */
- register char *s1, *s2;
- register int n;
- {
- while( chartab[(*s1++) & 0177] == chartab[(*s2++) & 0177] && --n > 0)
- ;
- return n == 0;
- }
-
-
- char *vlookup(val, table) /* lookup up int a give corresponding str */
- register int val;
- register Lookup *table;
- {
- while( table->str != 0)
- {
- if( table->value == val)
- return table->str;
- table++;
- }
- return (char *)0;
- }
-
- int slookup(str, table) /* lookup string in table */
- register char *str;
- register Lookup *table;
- {
- register int len1;
- register int len2;
-
- if (str == 0)
- return -1;
- else
- len1 = strlen(str);
- while( table->str != 0)
- {
- len2 = strlen(table->str);
- if( lexequ(str, table->str, max(len1,len2) ) )
- return table->value;
- table++;
- }
- return -1;
- }
-
- char *lowerfy(s) /* tr A-Z a-z */
- register char *s;
- {
- register char *p = s;
-
- while(*p)
- {
- if( isupper(*p) )
- *p = tolower(*p);
- p++;
- }
- return s;
- }
-
- char *getfpath(str) /* get full pathname + allocate storage */
- char *str;
- {
- char pathname[256];
- /* We may be supplied with an absolute pathname, or a name relative to
- * the tailor variable directory (null -> don't prefix!).
- * Decide whether & how to prefix (suffix ?) str with pathname.
- */
-
- #ifdef VMS21
- /* dev: -> full pathname LEAVE ALONE
- * [name] -> full pathname LEAVE ALONE
- * sym$name -> symbolic name LEAVE ALONE
- * <else> -> normal PREFIX with directory
- */
- if (directory == NULL ||
- index(str, ':') || index(str, '[') || index(str, '$'))
- return strdup(str);
- else VOID sprintf( pathname, "%s%s", directory, str);
- return strdup(pathname);
- #else VMS21
- # ifdef PRIME
- /* > -> full pathname LEAVE ALONE
- * <else> -> normal PREFIX with directory>
- */
- if (directory == NULL || index(str, '>'))
- return strdup(str);
- else VOID sprintf( pathname, "%s>%s", directory, str);
- return strdup(pathname);
- # else PRIME
- /* Not VMS or PRIME -> must be UNIX !! */
-
- /* / -> full pathname LEAVE ALONE
- * ./ ../ -> relative pathname LEAVE ALONE
- * <else> -> Normal PREFIX with directory/
- */
- if( *str == '/' || directory == NULL ||
- lexequ(str, "../", 3) || lexequ(str,"./", 2) )
- return strdup(str);
- else VOID sprintf( pathname, "%s/%s", directory, str);
- return strdup(pathname);
- # endif PRIME
- #endif VMS21
- }
-
- void skipnl(fp) /* skip to to next line */
- register FILE *fp;
- {
- register int c;
-
- while((c = getc(fp)) != '\n' && c != EOF)
- ;
- if( c == EOF)
- fatal0("Unexpected end of file");
- }
-
- void
- consistant(fp, file) /* check headers for consistancy */
- FILE *fp;
- char *file;
- {
- Version temp;
-
- if( thisversion.day == 0 ) /* not initialised yet */
- {
- getversion(&thisversion, fp);
- if( verbose )
- {
- vprintf0(1, "NRS database - ");
- verprint( stderr, &thisversion);
- putc('\n', stderr);
- }
- return;
- }
- getversion(&temp, fp);
- if( thisversion.hours != temp.hours
- || thisversion.minutes != temp.minutes
- || thisversion.day != temp.day
- || thisversion.month != temp.month
- || thisversion.year != temp.year
- || thisversion.format != temp.format)
- {
- fprintf( stderr, "Versions differ - reference ");
- verprint(stderr, &thisversion);
- fprintf( stderr, "File %s ", file);
- verprint(stderr, &temp );
- fatal0("\nFiles are out of sync");
- }
- }
-
- static_f void
- getversion(vp, fp)
- Version *vp;
- FILE *fp;
- {
- int c;
-
- while( isspace(c = getc(fp)) )
- ;
- VOID ungetc(c, fp);
- /* Parse ``hh.mm ddmmyyFORMAT f''*/
- vp->hours = getint(fp, 2);
- vp->minutes = getint(fp, 3);
- vp->day = getint(fp, 3);
- vp->month = getint(fp, 2);
- vp->year = getint(fp, 2);
- vp->format = getint(fp, 8);
- }
-
- static_f void
- verprint(fp, vp)
- Version *vp;
- FILE *fp;
- {
- fprintf(fp, "version %02d:%02d %2d/%02d/19%02d format%2d",
- vp->hours,
- vp->minutes,
- vp->day,
- vp->month,
- vp->year,
- vp->format);
- }
-
- void
- addcomments(fp, name, rest)
- FILE *fp;
- char *name, *rest;
- {
- FILE *cfp;
- char buffer[256], *p, *ctime();
-
- if((cfp = fopen(commentfile, "r")) == NULL)
- {
- warn1(1, "Can't open comments file %s", commentfile);
- return;
- }
- while( fgets(buffer, sizeof buffer, cfp) != NULL)
- {
- for(p = buffer; *p; p++)
- {
- if( *p != MAGIC )
- {
- putc(*p, fp);
- continue;
- }
- if( *(p + 2) != MAGIC || *(p+1) == '\0')
- {
- putc(*p, fp);
- continue;
- }
- switch(*(p+1))
- {
- case 'D': /* the date */
- {
- time_t now;
-
- VOID time(&now);
- fprintf(fp, "%.24s", ctime(&now));
- break;
- }
- case 'H': /* the hostname */
- if( hostname )
- fputs(hostname, fp);
- break;
- case 'N': /* the nrs data */
- verprint(fp, &thisversion);
- break;
- case 'F': /* the file */
- fputs(name, fp);
- break;
- case 'C': /* any other string */
- fputs(rest, fp);
- break;
- }
- p += 2;
- }
- }
- VOID fclose(cfp);
- }
-
- #ifdef VMS21
- char *
- index(string, ch)
- register char *string;
- register char ch;
- {
- while (*string) if (*string++ == ch) return --string;
- return (char *) 0;
- }
- #endif VMS21
-